import {createRouter, createWebHistory} from 'vue-router'
import {unauthorized} from "@/net/index.js";
import Layout from '../views/Layout/index.vue'
import Home from '../views/Home/index.vue'
import MyInterface from '../views/MyInterface/index.vue'
import ManageCenter from '../views/ManageCenter/index.vue'
import UserManage from '../views/ManageCenter/Manage/UserManage.vue'
import InterfaceManage from '../views/ManageCenter/Manage/InterfaceManage.vue'
import InterfaceStatistics from '../views/InterfaceStatistics/index.vue'
import AboutMe from '../views/AboutMe/index.vue'
import InterfaceInfo from '../views/InterfaceInfo/index.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/apiShop',
            component: Layout,
            children: [
                {
                    path: '/apiShop',
                    name: 'apiShop',
                    component: () => import("@/views/Home/index.vue"),
                    meta: {
                        title: 'API商店',
                    }
                },
                {
                    path: '/myInterface',
                    name: 'myInterface',
                    component: () => import("@/views/MyInterface/index.vue"),
                    meta: {
                        title: '我的接口',
                    }
                },
                {
                    path: '/interfaceInfo',
                    name: 'interfaceInfo',
                    component: () => import("@/views/InterfaceInfo/index.vue"),
                    meta: {
                        title: '接口文档',
                    }
                },
                {
                    path: '/manageCenter',
                    name: 'manageCenter',
                    component: () => import("@/views/ManageCenter/index.vue"),
                    children: [
                        {
                            path: 'userManage',
                            name: 'userManage',
                            component: () => import("@/views/ManageCenter/Manage/UserManage.vue"),
                            meta: {
                                title: '用户管理',
                            }
                        },
                        {
                            path: 'interfaceManage',
                            name: 'interfaceManage',
                            component: () => import("@/views/ManageCenter/Manage/InterfaceManage.vue"),
                            meta: {
                                title: '接口管理',
                            }
                        },
                    ],
                    meta: {
                        title: '管理中心',
                    }
                },
                {
                    path: '/interfaceStatistics',
                    name: 'interfaceStatistics',
                    component: () => import("@/views/InterfaceStatistics/index.vue"),
                    meta: {
                        title: '接口统计',
                    }
                },
                {
                    path: '/aboutMe',
                    name: 'aboutMe',
                    component: () => import("@/views/AboutMe/index.vue"),
                    meta: {
                        title: '个人中心',
                    }
                },
            ],
            meta: {
                title: '首页',
            }
        },
        {
            path: '/',
            name: 'website-index',
            component: () => import("@/views/Login/LoginIndex.vue"),
            children: [
                {
                    path: '',
                    name: 'homePage-login',
                    component: () => import('@/views/Login/welcome/LoginPage.vue')
                },
                {
                    path: '/register',
                    name: 'homePage-register',
                    component: () => import('@/views/Login/welcome/RegisterPage.vue')
                }
            ],
            meta: {
                title: '登录',
            }
        },
    ]
})

router.beforeEach((to, from, next) => {
    const isUnauthorized = unauthorized()
    if(to.name.startsWith('homePage') && !isUnauthorized) {
        next('/apiShop')
    } else if(to.fullPath.startsWith('/apiShop') && isUnauthorized) {
        next('/')
    } else {
        next()
    }
})

export default router
